package com.xy.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xy.entity.ProductType;
import com.xy.entity.User;
import com.xy.exception.P2PException;
import com.xy.service.ProductTypeService;

@Controller
public class ProductTypeController {
	private Logger log = Logger.getLogger(ProductTypeController.class);
	@Autowired
	private ProductTypeService productTypeService;

	@RequestMapping(value = "addProductType", method = RequestMethod.POST)
	public String addProductType(@Validated ProductType productType, HttpSession session) {
		User user = (User) session.getAttribute("myUser");
		productType.setCreaterTime(new Date());
		productType.setCreaterID(user.getID());
		if (productTypeService.addProductType(productType)) {
			log.info("产品类型添加成功：" + productType);
		}
		return "selectProductType.action";
	}

	/**
	 * 更新产品类型
	 * 
	 * @param productType
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "updateProductType", method = RequestMethod.POST)
	public String updateProductType(@Validated ProductType productType, HttpSession session) {
		User user = (User) session.getAttribute("myUser");
		productType.setCreaterTime(new Date());
		productType.setCreaterID(user.getID());
		if (productTypeService.updateProductType(productType)) {
			log.info("产品类型修改成功：" + productType);
			return "selectProductType.action";
		}
		log.info("产品类型修改失败：" + productType);
		return "selectProductType.action";
		// new P2PException("111", "更新失败！");
	}

	/**
	 * 更新产品类型状态
	 * 
	 * @param productType
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "updateProductTypeStatus", method = RequestMethod.GET)
	public @ResponseBody String updateProductTypeStatus(ProductType productType, HttpSession session) {
		User user = (User) session.getAttribute("myUser");
		List<ProductType> productTypeList = productTypeService.selectProductType(productType);
		productType = productTypeList.get(0);
		productType.setCreaterTime(new Date());
		productType.setCreaterID(user.getID());
		if (productType.getProductTypeStatus() == 0) {
			productType.setProductTypeStatus(1);
		} else {
			productType.setProductTypeStatus(0);
		}
		if (productTypeService.updateProductType(productType)) {
			return "success";
		}
		return "error";
	}

	/**
	 * 查询所有
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "selectProductType", method = { RequestMethod.GET, RequestMethod.POST })
	public String selectProductType(Model model) {
		List<ProductType> productTypeList = productTypeService.selectProductType(null);
		model.addAttribute("productTypeList", productTypeList);
		return "pages/backend/productTypeManage.jsp";
	}
}
